package com.mushuilingfeng.ddd.user.domain.base;

import com.mushuilingfeng.ddd.framework.entity.IDomainEntity;
import com.mushuilingfeng.ddd.framework.factory.DomainIdFactory;
import lombok.Data;

import java.util.Date;

/**
 * @author
 * @date 2024/9/18
 */
@Data
public class BaseEntity implements IDomainEntity {
    protected Long id;
    protected Date createTime;
    protected Date updateTime;

    public void setId(Long id) {
        if (null != this.id) {
            throw new IllegalArgumentException("ID值已经初始化，不允许修改！");
        }
        this.id = id;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void saveNew() {
        simpleCheckParams();
        if (null == this.id) {
            this.id = DomainIdFactory.nextId();
        }
        if (null == this.createTime) {
            this.createTime = new Date();
        }
        if (null == this.updateTime) {
            this.updateTime = new Date();
        }
    }

    @Override
    public void update() {
        IDomainEntity.super.update();
        simpleCheckParams();
    }
}
